/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.service.document;

import cn.ac.nercis.pes.model.dto.common.Result;
import cn.ac.nercis.pes.model.dto.common.ResultCode;
import cn.ac.nercis.pes.model.dto.evaluation.analyze.TargetScoreDTO;
import cn.ac.nercis.pes.model.dto.report.ScoreStatDTO;
import cn.ac.nercis.pes.repository.dal.project.SystemRepository;
import cn.ac.nercis.pes.repository.model.project.SystemDO;
import cn.ac.nercis.pes.service.document.report.ReportOverallEvaluationDataService;
import cn.ac.nercis.pes.service.utils.CalculateUtils;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 统计服务
 * @author zpy
 * @version 1.0
 */
@Slf4j
@Service
public class StatisticService {
    private final SystemRepository systemRepository;
    private final ReportOverallEvaluationDataService overallEvaluationDataService;

    @Autowired
    public StatisticService(SystemRepository systemRepository,
                            ReportOverallEvaluationDataService overallEvaluationDataService) {
        this.systemRepository = systemRepository;
        this.overallEvaluationDataService = overallEvaluationDataService;
    }

    public Result<ScoreStatDTO> getSystemScoreInfo(@NonNull String systemId){
        SystemDO systemDO = systemRepository.findById(systemId).orElse(null);
        if(Objects.isNull(systemDO)){
            return Result.failed(ResultCode.SYSTEM_NOT_EXIST);
        }
        List<TargetScoreDTO> layerScores = overallEvaluationDataService.getLayerScore(systemDO,true);
        return Result.success(ScoreStatDTO.builder()
                .layerScores(layerScores)
                .overallScore(CalculateUtils.getOverallScore(layerScores))
                .build());
    }

}